package com.cgw.game.adapter;

import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.cgw.game.R;
import com.cgw.game.entity.PrescriptionAnswerEntity;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * @类描述 describe
 * @作者 YF
 * @创建时间 2019/11/6 17:35
 */
public class PrescriptionAnswerAdapter extends BaseMultiItemQuickAdapter<PrescriptionAnswerEntity, BaseViewHolder> {
    private Listener listener;
    private boolean isShow = false;
    private boolean isload = false;

    public void setIsload(boolean isload) {
        this.isload = isload;
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public PrescriptionAnswerAdapter(List<PrescriptionAnswerEntity> data) {
        super(data);
        addItemType(0,R.layout.problem_item);
        addItemType(1,R.layout.answer_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, final PrescriptionAnswerEntity item) {
        switch (item.getItemType()){
            case 0:
                helper.setText(R.id.content,item.getTitle());
                helper.getView(R.id.start).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(listener!=null&&!isload){
                            isload = true;
                            isShow = true;
                            listener.start();
                        }
                    }
                });
                if(isShow){
                    helper.getView(R.id.line1).setVisibility(View.GONE);
                    helper.getView(R.id.start).setVisibility(View.GONE);
                }else {
                    helper.getView(R.id.line1).setVisibility(View.VISIBLE);
                    helper.getView(R.id.start).setVisibility(View.VISIBLE);
                }

                break;
            case 1:
                helper.setText(R.id.content,item.getTitle());
                if(item.getContent()!=null){
                    helper.getView(R.id.content).setVisibility(View.GONE);
                    helper.getView(R.id.rg_group).setVisibility(View.VISIBLE);
                    RadioGroup radioGroup = helper.getView(R.id.rg_group);
                    RadioButton rb1 = helper.getView(R.id.rb1);
                    RadioButton rb2 = helper.getView(R.id.rb2);
                    RadioButton rb3 = helper.getView(R.id.rb3);
                    RadioButton rb4 = helper.getView(R.id.rb4);
                    if(item.getContent().size()==2){
                        rb1.setVisibility(View.VISIBLE);
                        rb1.setText(item.getContent().get(0));
                        rb2.setVisibility(View.VISIBLE);
                        rb2.setText(item.getContent().get(1));
                        rb3.setVisibility(View.GONE);
                        rb4.setVisibility(View.GONE);
                    }else if(item.getContent().size()==3){
                        rb1.setVisibility(View.VISIBLE);
                        rb1.setText(item.getContent().get(0));
                        rb2.setVisibility(View.VISIBLE);
                        rb2.setText(item.getContent().get(1));
                        rb3.setVisibility(View.VISIBLE);
                        rb3.setText(item.getContent().get(2));
                        rb4.setVisibility(View.GONE);
                    }else {
                        rb1.setVisibility(View.VISIBLE);
                        rb1.setText(item.getContent().get(0));
                        rb2.setVisibility(View.VISIBLE);
                        rb2.setText(item.getContent().get(1));
                        rb3.setVisibility(View.VISIBLE);
                        rb3.setText(item.getContent().get(2));
                        rb4.setVisibility(View.VISIBLE);
                        rb4.setText(item.getContent().get(3));
                    }

                    rb1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(listener!=null&&!isload){
                                isload = true;
                                item.setSelect(0);
                                listener.next(item.getPos());
                            }
                        }
                    });
                    rb2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(listener!=null&&!isload){
                                isload = true;
                                item.setSelect(1);
                                listener.next(item.getPos());
                            }
                        }
                    });
                    rb3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(listener!=null&&!isload){
                                isload = true;
                                item.setSelect(2);
                                listener.next(item.getPos());
                            }
                        }
                    });
                    rb4.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(listener!=null&&!isload){
                                isload = true;
                                item.setSelect(3);
                                listener.next(item.getPos());
                            }
                        }
                    });

                    switch (item.getSelect()){
                        case 0:
                            radioGroup.check(R.id.rb1);
                            break;
                        case 1:
                            radioGroup.check(R.id.rb2);
                            break;
                        case 2:
                            radioGroup.check(R.id.rb3);
                            break;
                        case 3:
                            radioGroup.check(R.id.rb4);
                            break;
                        case -1:
                            radioGroup.clearCheck();
                            break;
                    }
                    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            switch (checkedId){
                                case R.id.rb1:
                                    item.setSelect(0);
                                    break;
                                case R.id.rb2:
                                    item.setSelect(1);
                                    break;
                                case R.id.rb3:
                                    item.setSelect(2);
                                    break;
                                case R.id.rb4:
                                    item.setSelect(3);
                                    break;
                            }
                        }
                    });
                }else {
                    helper.getView(R.id.content).setVisibility(View.VISIBLE);
                    helper.getView(R.id.rg_group).setVisibility(View.GONE);
                }
                break;
        }
    }

    public interface Listener{
        void start();
        void next(int pos);
    }
}
